package com.naiterui.ehp.bs.agent.interceptor;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.naiterui.ehp.bp.support.security.AgentLoginInterceptor;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bp.utils.context.SpringContextHolder;
import com.naiterui.ehp.bs.agent.service.security.ISecurityService;
import com.naiterui.ehp.bs.agent.service.security.impl.AgentSecurityServiceImpl;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class LoginInterceptor extends AgentLoginInterceptor {

    @Override
    protected boolean verifyToken(HttpServletRequest request, HttpServletResponse response) throws
            NumberFormatException,
            IOException {
        String agentIdStr = request.getHeader(SettingConfig.AGENT_ID);
        String token = request.getParameter(SettingConfig.TOKEN);
        ISecurityService securityService = SpringContextHolder.getBean(AgentSecurityServiceImpl.class);
        Long agentId = null;
        try{
            agentId = Long.parseLong(agentIdStr);
        }catch (Exception e){}
        return securityService.saveCheckToken(agentId, token, response);
    }


}